package com.cy5;

import java.util.*;

public class BinaryTree {
    public TreeNode root;

    static class TreeNode {
        public int val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用


        public TreeNode(int val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        root = node1;
        node1.left = node2;
        node2.left = node3;
        node1.right = node4;
        node4.left = node5;
        node4.right = node6;
        return root;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static int nodeSize;

    /**
     * 获取树中节点的个数：遍历思路
     */
    void size(TreeNode root) {
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        return -1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    int getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return leafSize;
    }

    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left != null || root.right != null) {
            return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
        }
        return 1;
    }

    /*
    获取第K层节点的个数
     */
    int getKLevelNodeCount(TreeNode root, int k) {
        if (k < 0 || root == null) {
            return 0;
        }
        if (k > 1) {
            return getKLevelNodeCount(root.right, k - 1) + getKLevelNodeCount(root.left, k - 1);
        } else {
            return 1;
        }
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
        }
    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        find(root.left, val);
        find(root.right, val);
        return null;
    }

    //层序遍历,用队列实现
    void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> qu = new LinkedList<TreeNode>();
        qu.offer(root);
        while (!qu.isEmpty()) {
            TreeNode cur = qu.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                qu.offer(cur.left);
            }
            if (cur.right != null) {
                qu.offer(cur.right);
            }
        }
    }


    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> qu = new LinkedList<TreeNode>();
        qu.offer(root);
        while (!qu.isEmpty()) {
            TreeNode cur = qu.poll();
            if (cur != null) {
                qu.offer(cur.left);
                qu.offer(cur.right);
            } else {
                break;
            }
        }
        while (!qu.isEmpty()) {
            TreeNode cur = qu.poll();
            if (cur != null) {
                return false;
            }
        }
        return true;
    }

    //判断两颗二叉树是否相同
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null || q == null) {
            if (p == q) {
                return true;
            } else {
                return false;
            }
        }
        //根节点
        if (p.val != q.val) {
            return false;
        }
        //左子树
        if (!isSameTree(p.left, q.left)) {
            return false;
        }
        //右子树
        if (!isSameTree(p.right, q.right)) {
            return false;
        }
        //到这里，一定是非空，左右子树都相同，返回true；
        return true;
    }

    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return judge(root.left, root.right);
    }

    public static boolean judge(TreeNode l, TreeNode r) {
        if (l == null && r == null) {
            return true;
        }
        if (l == null || r == null) {
            return false;
        }
        if (l.val != r.val) {
            return false;
        }
        if (!judge(l.left, r.right)) {
            return false;
        }
        if (!judge(l.right, r.left)) {
            return false;
        }
        return true;
    }

    //获取二叉树得最大深度
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> q = new LinkedList();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            List<Integer> tmp = new ArrayList<>(size);
            while (size > 0) {
                TreeNode cur = q.poll();
                if (cur.left != null) {
                    q.offer(cur.left);
                }
                if (cur.right != null) {
                    q.offer(cur.right);
                }
                tmp.add(cur.val);
                size--;
            }
            list.add(tmp);
        }
        return list;
    }

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> q = new LinkedList();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            List<Integer> tmp = new ArrayList<>(size);
            while (size > 0) {
                TreeNode cur = q.poll();
                if (cur.left != null) {
                    q.offer(cur.left);
                }
                if (cur.right != null) {
                    q.offer(cur.right);
                }
                tmp.add(cur.val);
                size--;
            }
            list.add(tmp);
        }
        Collections.reverse(list);
        return list;

    }
    StringBuilder sb=new StringBuilder();
    public String tree2str(TreeNode root) {
        if(root==null){
            return sb.toString();
        }
        if(root.left!=null){
            sb.append(root.val);
            sb.append("(");
            tree2str(root.left);
            sb.append(")");
        }else{
            if(root==null){
                return sb.toString();
            }
        }
        if(root.right==null){
            return sb.toString();
        }else{
            sb.append(root.val);
            sb.append("(");

            tree2str(root.right);
            sb.append(")");

        }
        return sb.toString();
    }
    //二叉搜索转化成双向链表
//    public TreeNode Convert(TreeNode root) {
//
//
//
//
//    }

    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        bt.createTree();
//        bt.preOrder(bt.root);
//        System.out.println();
//        bt.inOrder(bt.root);
//        System.out.println();
//        bt.postOrder(bt.root);
//        System.out.println(bt.getLeafNodeCount1(bt.root));
//        System.out.println(bt.getLeafNodeCount2(bt.root));
//        System.out.println(bt.getHeight(bt.root));

//        System.out.println(bt.getKLevelNodeCount(bt.root, 3));
//        System.out.println(bt.levelOrder2(bt.root));
    }
}